using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Pampa.Common.Pages;

namespace Pampa.WebControls
{
    #region Floating Window

    [DesignerAttribute(typeof(FloatingWindowDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public class FloatingWindow : Panel
    {
        private string title;
        private bool moveableBody;
        private string onClientClose;
        private bool closeButton;

        #region Constructores

        public FloatingWindow()
        {
            this.CssClass = "floatingWindowDefault";
            this.MoveableBody = false;
            this.CloseButton = true;
        }

        public FloatingWindow(string ID) : this()
        {
            this.ID = ID;
        }

        #endregion

        #region Properties

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public bool CloseButton
        {
            get { return closeButton; }
            set { closeButton = value; }
        }

        public bool MoveableBody
        {
            get { return moveableBody; }
            set { moveableBody = value; }
        }

        public string OnClientClose
        {
            get { return onClientClose; }
            set { onClientClose = value; }
        }
        
        #endregion

        #region Load

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.CssClass == "floatingWindowDefault")
                AbstractPage.RegisterStyleSheet(this.Page, "Pampa.WebControls/FloatingWindow.css");

            AbstractPage.RegisterLibrary(this.Page, "Pampa.WebControls/Drag.js");
            AbstractPage.RegisterLibrary(this.Page, "Pampa.WebControls/FloatingWindow.js");
        }

        #endregion

        #region Render

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddAttribute("id", this.ID);
            writer.AddAttribute("name", this.ID);
            writer.AddAttribute("class", this.CssClass);
            writer.AddAttribute("floatingWindow", "true");
            writer.AddAttribute("onselectstart", "return false;");
            writer.AddStyleAttribute("display", "none");

            if (!string.IsNullOrEmpty(this.Height.ToString()))
                writer.AddStyleAttribute("height", this.Height.ToString());
            if (!string.IsNullOrEmpty(this.Width.ToString()))
                writer.AddStyleAttribute("width", this.Width.ToString());

            if (this.MoveableBody)
            {
                writer.AddStyleAttribute("cursor", "move");
                writer.AddAttribute("onmousedown", "FloatingWindow_DragStart('" + this.ID + "', event)");
            }

            writer.AddStyleAttribute("position", "absolute");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            // Arma la tabla de encabezado del control (Top bar).
            writer.WriteLine("<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
            writer.WriteLine("<tr>");
            writer.WriteLine("<td class=\"titleBar\" onmousedown=\"FloatingWindow_DragStart('" + this.ID + "', event)\" align=\"left\" width=\"97%\" style=\"cursor:move\">" + this.title + "</td>");
            writer.WriteLine("<td align=\"right\" width=\"3%\">");

            if (this.CloseButton)
                writer.WriteLine("<div class=\"closeButton\" classRollOver=\"closeButtonOn\" onmouseover=\"Common.on(this)\" onmouseout=\"Common.off(this)\" onclick=\"FloatingWindow_Get('" + this.ID + "').hide();\"></td>");
            else
                writer.WriteLine("&nbsp;");

            writer.WriteLine("</tr>");
            writer.WriteLine("</table>");
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("</div>");

            RenderDefinitions(writer);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            RenderBeginTag(writer);
            RenderContents(writer);
            RenderEndTag(writer);
        }

        private void RenderDefinitions(HtmlTextWriter output)
        {
            // Instancia el objeto JavaScript.
            output.WriteLine("<script type=\"text/javascript\">");
            output.WriteLine("var obj" + this.ID + " = new FloatingWindow('" + this.ID + "');");
            output.WriteLine("obj" + this.ID + ".onClose = '" + this.onClientClose + "';");
            output.WriteLine("</script>");
        }

        #endregion

        #region MVC Hook

        public string RenderBeginTag()
        {
            var stringWriter = new StringWriter();
            var textWriter = new HtmlTextWriter(stringWriter);

            this.RenderBeginTag(textWriter);

            return stringWriter.ToString();
        }

        public string RenderEndTag()
        {
            var stringWriter = new StringWriter();
            var textWriter = new HtmlTextWriter(stringWriter);

            this.RenderEndTag(textWriter);

            return stringWriter.ToString();
        }

        #endregion
    }

    #endregion

    #region Designer

    public class FloatingWindowDesigner : System.Web.UI.Design.ContainerControlDesigner
    {
        public override string GetDesignTimeHtml()
        {
            return base.GetDesignTimeHtml();
        }

        public override bool AllowResize
        {
            get { return true; }
        }
    }

    #endregion
}
